package com.house.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.house.common.core.controller.BaseController;
import com.house.common.core.domain.AjaxResult;
import com.house.common.core.domain.entity.DictData;
import com.house.common.core.page.TableDataInfo;
import com.house.common.utils.StringUtils;
import com.house.system.service.IDictDataService;
import com.house.system.service.IDictTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 字典数据Controller
 * 
 * @author tc
 * @date 2022-02-04
 */
@RestController
@RequestMapping("/entity/dict/data")
public class DictDataController extends BaseController
{
    @Autowired
    private IDictDataService dictDataService;

    @Autowired
    private IDictTypeService dictTypeService;

    /**
     * 查询字典数据列表
     */
    @PreAuthorize("@ss.hasPermi('entity:dict:list')")
    @GetMapping("/list")
    public TableDataInfo list(DictData dictData)
    {
        startPage();
        List<DictData> list = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }


    /**
     * 获取字典数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('entity:dict:query')")
    @GetMapping(value = "/{dictCode}")
    public AjaxResult getInfo(@PathVariable("dictCode") Long dictCode)
    {
        return AjaxResult.success(dictDataService.selectDictDataByDictCode(dictCode));
    }

    /**
     * 新增字典数据
     */
    @PreAuthorize("@ss.hasPermi('entity:dict:add')")
    @PostMapping
    public AjaxResult add(@RequestBody DictData dictData)
    {
        return toAjax(dictDataService.insertDictData(dictData));
    }

    /**
     * 修改字典数据
     */
    @PreAuthorize("@ss.hasPermi('entity:dict:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody DictData dictData)
    {
        return toAjax(dictDataService.updateDictData(dictData));
    }

    /**
     * 删除字典数据
     */
    @PreAuthorize("@ss.hasPermi('entity:dict:remove')")
	@DeleteMapping("/{dictCodes}")
    public AjaxResult remove(@PathVariable Long[] dictCodes)
    {
        dictDataService.deleteDictDataByDictCodes(dictCodes);
        return success();
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType)
    {
        List<DictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data))
        {
            data = new ArrayList<DictData>();
        }
        return AjaxResult.success(data);
    }
}
